package StudentSystemDemo;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 阿宾
 * Date: 2024-09-23
 * Time: 22:08
 */
public class StudentDemo {
    public static void main(String[] args) {
        //为了便于管理学生信息，使用集合
        ArrayList<Student> list = new ArrayList<>();
        while (true) {
            System.out.println("------------欢迎来到学生管理系统-------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出");
            System.out.println("请输入您的选择:");
            Scanner scanner = new Scanner(System.in);
            String input = scanner.next();
            switch (input){
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudent(list);
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent(list);
                    break;
                case "4":
                    //System.out.println("查询学生");
                    queryStudent(list);
                    break;
                case "5":
                    /*System.out.println("退出");
                    break loop;*/
                    System.exit(0);
                default:
                    System.out.println("输入有错，请重新输入");
            }
        }
    }
    //1.添加功能
    public static void addStudent(ArrayList<Student> list){
        //创建一个空的学生对象
        Student student = new Student();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("请输入要添加的学生id:");
            String id = scanner.next();
            boolean ret = check(list,id);
            if(ret){
                System.out.println("输入的学生id重复，请重新输入");
            }else {
                student.setId(id);
                break;
            }
        }

        System.out.println("请输入要添加的学生姓名:");
        String name = scanner.next();
        student.setName(name);

        System.out.println("请输入要添加的学生年龄:");
        int age = scanner.nextInt();
        student.setAge(age);

        System.out.println("请输入要添加的学生家庭住址:");
        String address = scanner.next();
        student.setAddress(address);

        //添加到集合中
        list.add(student);

    }

    //检查id在list中是否存在
    public static boolean check(ArrayList<Student> list,String id){
        /*for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)) {
                return true;
            }
        }
        return false;*/
        return isExist(list,id) >= 0;
    }
    //2.删除功能
    public static void deleteStudent(ArrayList<Student> list){
        System.out.println("请输入要删除的学生id:");
        Scanner scanner = new Scanner(System.in);
        String id = scanner.next();

        int ret = isExist(list,id);
        if(ret >= 0){
            //id在集合中存在，需要删除
            list.remove(ret);
            System.out.println("删除成功");
        }else {
            System.out.println("id不存在");
        }

    }

    //返回id在list中的下标
    public static int isExist(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                return i;
            }
        }
       return -1;
    }
    //3.修改功能
    public static void updateStudent(ArrayList<Student> list){
        System.out.println("请输入要修改的学生id:");
        Scanner scanner = new Scanner(System.in);
        Student student = new Student();
        String id = scanner.next();
        int index = isExist(list,id);
        if(index >= 0){
            student.setId(id);
            System.out.println("请输入要修改的学生姓名：");
            String name = scanner.next();
            student.setName(name);

            System.out.println("请输入要修改的学生年龄：");
            int age = scanner.nextInt();
            student.setAge(age);

            System.out.println("请输入要修改的学生家庭住址：");
            String address = scanner.next();
            student.setAddress(address);

            list.set(index,student);
            System.out.println("修改信息成功！");
        }else {
            System.out.println("修改的学生id不存在，请重新输入");
        }
    }

    //4.查询功能
    public static void queryStudent(ArrayList<Student> list){
        if(list.size() == 0){
            System.out.println("当前无学生信息，请添加后在查询");
            return;
        }
        System.out.println("id\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId() + "\t" + stu.getName() + "\t"+ stu.getAge() + "\t"+ stu.getAddress());
        }

    }
}
